import { Controller, Get, Post, Body, Param, Query, UseGuards, SetMetadata, Req } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto, DeleteUserDto, EditUserRoles, FindManyUserDto, FindOneUserDto, FrozenUserDto, LoginUserDto, RegisterCaptchaDto, UpdateInfoCaptchaDto, UpdatePass, UpdateUserInfoDto } from './dto/user.dto';
import { ApiTags } from '@nestjs/swagger';
import { FastifyRequest } from 'fastify';

@ApiTags('用户')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Post("register")
  @SetMetadata('not-require-login', true)
  register(@Body() cud: CreateUserDto) {
    return this.userService.register(cud);
  }

  @Post("delete")
  @SetMetadata('require-permission', ['superAdmin'])
  delete(@Body() dud: DeleteUserDto) {
    return this.userService.remove(dud);
  }

  @Get("findMany")
  @SetMetadata('require-permission', ['superAdmin'])
  findMany(@Query() fmud: FindManyUserDto) {
    return this.userService.findMany(fmud);
  }

  @Get('findOne')
  findOne(@Query() foud: FindOneUserDto) {
    return this.userService.findOne(foud);
  }

  @Get('getUserInfo')
  getUserInfo(@Req() req: FastifyRequest) {
    return this.userService.getUserInfo(req);
  }

  // 验证码
  @Post('captcha')
  @SetMetadata('not-require-login', true)
  captcha(@Body() rcd: RegisterCaptchaDto) {
    return this.userService.captcha(rcd);
  }

  @Post('login')
  @SetMetadata('not-require-login', true)
  async login(@Body() loginDto: LoginUserDto) {
    return this.userService.login(loginDto)
  }

  @Post("editUserRoles")
  @SetMetadata('require-permission', ['superAdmin'])
  editUserRoles(@Body() eurs: EditUserRoles) {
    return this.userService.editUserRoles(eurs)
  }

  @Post("updateUserInfo")
  updateUserInfo(@Body() uuid: UpdateUserInfoDto) {
    return this.userService.updateUserInfo(uuid)
  }

  @Post("frozenUser")
  @SetMetadata('require-permission', ['superAdmin'])
  frozenUser(@Body() fud: FrozenUserDto) {
    return this.userService.frozenUser(fud)
  }


  @Post("updateInfoCaptcha")
  updateInfoCaptcha(@Body() uicd: UpdateInfoCaptchaDto) {
    return this.userService.updateInfoCaptcha(uicd)
  }

  @Post("updatePass")
  @SetMetadata('not-require-login', true)
  updatePass(@Body() up: UpdatePass) {
    return this.userService.updatePass(up)
  }
}
